import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/Home.vue'
import { handleLocalStorage } from '../utils/handleLocalStorage.js'

const routes = [
    {
        path: '/',
        name: 'home',
        component: Home
    },
    {
        path: '/listview',
        name: 'listview',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import('../views/ListView.vue')
    },
    {
        path: '/search',
        name: 'search',
        component: () => import('../views/Search.vue')
    },
    {
        path: '/searchdetail',
        name: 'searchdetail',
        component: () => import('../views/SearchDetail.vue')
    },
    {
        path: '/login',
        name: 'login',
        beforeEnter: (to, from, next) => {
            // ...
            const userInfo = handleLocalStorage('get', 'userInfo');
            if (userInfo) {
                next('/me');
            } else {
                next()
            }
        },
        component: () => import('../views/Login.vue')
    },
    {
        path: '/me',
        name: 'me',
        beforeEnter: (to, from, next) => {
            // ...
            const userInfo = handleLocalStorage('get', 'userInfo');
            if (userInfo) {
                next();
            } else {
                next('/login')
            }
        },
        component: () => import('../views/Me.vue')
    },
    {
        path: '/ranklist',
        name: 'ranklist',
        component: () => import('../views/Rank.vue')
    },
    {
        path: '/recommend',
        name: 'recommend',
        component: () => import('../views/DailyRecom.vue')
    }
]

const router = createRouter({
    // history: createWebHistory(process.env.BASE_URL),
    history: createWebHashHistory(process.env.BASE_URL),
    routes
})

export default router